package com.qhad.ads.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.songheng.eastnews.apk.R;
import com.juwang.girl.util.QihuAdvertis;
import com.qhad.ads.sdk.adcore.Qhad;
import com.qhad.ads.sdk.interfaces.IQhAdEventListener;
import com.qhad.ads.sdk.interfaces.IQhNativeBannerAd;


@SuppressWarnings("unused")
public class NativeBannerActivity extends Activity {

    private LinearLayout.LayoutParams nativebanneradlp;
    private LinearLayout nativebannerAdContainer;
    private IQhNativeBannerAd nativeBannerAd;
    private RelativeLayout rlContainer;
    private RadioButton rbAspRate;
    private RadioButton rbWh;
    private int containerWidth;
    private boolean isAddedAds = false;
    private TextView tvInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.nativebanner);

        setTitle("嵌入式广告");

        rbAspRate = (RadioButton) findViewById(R.id.rbAspRate);
        rbWh = (RadioButton) findViewById(R.id.rbWh);
        rbWh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                rbAspRate.setChecked(!isChecked);
            }
        });
        rbAspRate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                rbWh.setChecked(!isChecked);
            }
        });
        rlContainer = (RelativeLayout) findViewById(R.id.rlContainer);

        rlContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                containerWidth = rlContainer.getMeasuredWidth();

                /**
                 * 显示广告
                 */
                if (!isAddedAds) {
                    addAds();
                    isAddedAds = true;
                }
            }
        });

        findViewById(R.id.btnToLV).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(NativeBannerActivity.this, NativeBannerInListViewActivity.class);

                EditText etAspW = (EditText) findViewById(R.id.etAspRateW);
                EditText etAspH = (EditText) findViewById(R.id.etAspRateH);

                intent.putExtra("width", etAspW.getText().toString());
                intent.putExtra("height", etAspH.getText().toString());
                startActivity(intent);
            }
        });
        /**
         * 刷新广告
         */
        findViewById(R.id.btnLoad).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAds();
                tvInfo.setVisibility(View.VISIBLE);
            }
        });

        findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nativeBannerAd.closeAds();
                tvInfo.setVisibility(View.GONE);
            }
        });
    }

    private void addAds() {
        boolean isAspRate = rbAspRate.isChecked();
        boolean isWh = rbWh.isChecked();
        int height, width;
        height = 0;
        width = 0;
        if (isAspRate) {
            EditText etAspW = (EditText) findViewById(R.id.etAspRateW);
            EditText etAspH = (EditText) findViewById(R.id.etAspRateH);
            double dW = Double.parseDouble(etAspW.getText().toString());
            double dH = Double.parseDouble(etAspH.getText().toString());
            width = containerWidth;
            height = (int) Math.floor(containerWidth * (dH / dW));
        }
        if (isWh) {
            EditText etWidth = (EditText) findViewById(R.id.etWidth);
            EditText etHeight = (EditText) findViewById(R.id.etHeight);
            String strWidth = etWidth.getText().toString();
            String strHeight = etHeight.getText().toString();
            width = Integer.parseInt(strWidth);
            height = Integer.parseInt(strHeight);
        }

        /**
         * Key Code Start
         */
        final LinearLayout nativebannerAdContainer = new LinearLayout(this); // 广告容器 adContainer

        nativebannerAdContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                tvInfo = (TextView) findViewById(R.id.tvViewInfo);
                tvInfo.setText(String.format("W: %s px,H: %s px", nativebannerAdContainer.getMeasuredWidth(), nativebannerAdContainer.getMeasuredHeight()));
            }
        });

        final String adSpaceid = "PPub5d0djn"; // 广告位ID adSpaceid
//        final String adSpaceid = QihuAdvertis.interBannerid;
        nativeBannerAd = Qhad.showNativeBanner(nativebannerAdContainer, this, adSpaceid, false); // 请求广告

        /**
         * 设置广告回调
         */
        nativeBannerAd.setAdEventListener(new IQhAdEventListener() {
            @Override
            public void onAdviewGotAdSucceed() {

            }

            @Override
            public void onAdviewGotAdFail() {

            }

            @Override
            public void onAdviewRendered() {

            }

            @Override
            public void onAdviewIntoLandpage() {

            }

            @Override
            public void onAdviewDismissedLandpage() {

            }

            @Override
            public void onAdviewClicked() {

            }

            @Override
            public void onAdviewClosed() {

            }

            @Override
            public void onAdviewDestroyed() {

            }
        });

        rlContainer.removeAllViews();

        nativebanneradlp = new LinearLayout.LayoutParams(width, height); //设置 adContainer 的宽高
        rlContainer.addView(nativebannerAdContainer, nativebanneradlp);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        /**
         * Acvitivy销毁时调用，回收广告资源
         */
        Qhad.activityDestroy(this);
    }
}
